
import java.util.Arrays;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    //一左一右为最佳贪心策略
    public int candy(int[] ratings) {
            int[] candy=new int[ratings.length];
            int result=0;
        Arrays.fill(candy,1);
        for (int i = 1; i < ratings.length; i++) {
            if (ratings[i] > ratings[i - 1]) {
                candy[i] = candy[i - 1] + 1;
            }
        }
        for (int i = ratings.length - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                candy[i] = Math.max(candy[i + 1] + 1, candy[i]);
            }
        }

        for (int i = 0; i < candy.length; i++) {
            result+=candy[i];
        }
        return result;
    }
}
//leetcode submit region end(Prohibit modification and deletion)
